package com.mongodb.test;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.pool.MongoDriver;

public class ConcurrentTask implements Runnable {
	private static final MongoClient mongoClient = MongoDriver.mongoClient;
	private int taskNum;

	public ConcurrentTask(int taskNum) {
		this.taskNum = taskNum;
	}

	@Override
	public void run() {
		try {
			// 连接到数据库
			MongoDatabase mongoDatabase = mongoClient.getDatabase("tb_mongo_demo");
			MongoCollection<Document> collection = mongoDatabase.getCollection("mongoDB004");			
			synchronized (this) {
				
				for(int i = 0; i < 50000; i++) {
					Document document = new Document("title", "MongoDB").append("description", "database").append("taskNum", taskNum)
							.append("date", "2018-12-14");					
					collection.insertOne(document);			
				}				
			}
			System.out.println("文档插入成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("task:" + taskNum + "执行完毕");
	}

}
